<?php
require 'header.php';
if(!isset($_GET['item']))
{
	$query="SELECT name,part_no FROM item";
	$result=mysqli_query ($con, $query);
	echo "<h3>Modify Avaliable Stock Items</h3>";
	while($branch = mysqli_fetch_array($result))
	{	
		$link="modifyStock.php?item=".$branch['part_no'];
		echo "<br><a href=$link>$branch[name]</a>";
	}
}
else {
	if(isset($_POST['submit']))
	{
		$queryBranch="SELECT name FROM branch";
		$result = mysqli_query($con, $queryBranch);
		$branches=array('Downtown','Uptown');
		foreach ($branches as $branch)
		{
			$temp=$_POST[$branch];
			$query="UPDATE stock SET quantity=$temp WHERE itemID=".$_GET['item'] ." AND branch_name='".$branch."'";
			$result=mysqli_query ($con, $query);
		}
	}
		$query="SELECT * FROM stock WHERE itemID=".$_GET['item'];
		$result = mysqli_query($con, $query);
		echo '<form action="" method="post">';
		echo "<table border=1 cellpadding=10>";
		echo "<tr><th>Item ID</th><th>Branch</td><th>Current Quantity</th><th>New Quantity</th></tr>";
		while($items = mysqli_fetch_array($result))
			echo "<tr><td>".$items['itemID']."</td><td>".$items['branch_name']."</td><td>".$items['quantity']."</td><td><input type='text' name=".$items['branch_name']." size='4' value='".$items['quantity']."'></td></tr>";
		echo "</table>";
		echo '<input type="submit" name="submit" value="Update Stock">';
		echo '</form>';

}

require 'footer.php';					
?>
	